package com.example.service;

import com.example.common.Result;
import com.example.entity.User;
import com.example.entity.UserActions;
import com.example.entity.Video;
import com.example.mapper.UserActionMapper;
import com.example.mapper.VideoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class VideoService {
    @Resource
    VideoMapper videoMapper;
    @Resource
    UserActionMapper userActionMapper;
    public Result addVideo(Video video) {
        Date date = new Date();
        video.setVideoTime(date);
        if(!videoMapper.add(video)){
            return Result.error("新增失败");
        }
        return Result.success("新增成功");
    }

    public Result listByUser(User user) {
        List<Video> result = new ArrayList<>();
        List<Video> videoList = videoMapper.listByUser();
        List<UserActions> userActionsList = userActionMapper.getVideoByUser(user);
        Map<Integer,List<UserActions>> userActionsMap = userActionsList.stream().collect(Collectors.groupingBy(x -> x.getResourceId()));
        for (Video video : videoList){
            if(userActionsMap.get(video.getVideoId())!=null){
                video.setIfLike(1);
            }else {
                video.setIfLike(0);
            }
            result.add(video);
        }
        return Result.success(result);
    }

    public Result searchViedo(Video video) {
        List<Video> videoList = videoMapper.search(video);
        return Result.success(videoList);
    }

    public Result getViedo(Video video) {
        Video result = videoMapper.getById(video.getVideoId());
        Integer num = result.getVideoCount() + 1;
        result.setVideoCount(num);
        videoMapper.update(result);
        return Result.success(result);
    }

    public Result likeViedo(UserActions userActions) {
        Video result = videoMapper.getById(userActions.getResourceId());
        Integer num = result.getVideoLike() + 1;
        result.setVideoLike(num);
        videoMapper.update(result);

        Date date = new Date();
        userActions.setActionTime(date);
        userActionMapper.add(userActions);
        return Result.success(result);
    }

    public Result unLikeViedo(UserActions userActions) {
        Video result = videoMapper.getById(userActions.getResourceId());
        Integer num = result.getVideoLike() - 1;
        result.setVideoLike(num);
        videoMapper.update(result);

        userActionMapper.delete(userActions);
        return Result.success(result);
    }

    public Result updateVideo(Video video) {
        if(!videoMapper.update(video)){
            return Result.error("更新失败");
        }
        return Result.success("更新成功");
    }

    public Result listAll() {
        List<Video> videoList = videoMapper.listAll();
        return Result.success(videoList);
    }

    public Result delete(Video video) {
        if(!videoMapper.delete(video)){
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }
}
